// -----------------------------------------------------------
// Game Framework
// Copyright © 2024-2024 deadcycle. All rights reserved.
// Homepage: https://deadcycle.gitee.io
// Feedback: deadcycle@163.com
// -----------------------------------------------------------

using DG.Tweening;

using UnityEngine;
using UnityEngine.EventSystems;

namespace UIWidget
{
    /// <summary>
    /// 具有缩放效果的按钮。
    /// </summary>
    public class ScaleButton : ButtonEx
    {
        #region 字段
        /// <summary> 缩放比例。 </summary>
        private const float k_ScaleFactor = 1.08f;

        /// <summary> 缩放的持续时间。 </summary>
        private const float k_ScaleDuration = 0.15f;

        /// <summary> 按钮原始状态缩放比例。 </summary>
        private Vector3 m_OriginalScale = Vector3.zero;

        /// <summary> 按钮最终缩放比例。 </summary>
        private Vector3 m_FinalScale;

        /// <summary> 是否处于缩放中。 </summary>
        private bool m_IsScaling;

        /// <summary> 当前动画的Tween实例。 </summary>
        private Tween m_CurrentTween;

        // /// <summary> 当前动画的Tween动画曲线。 </summary>
        // private Ease m_Ease;
        #endregion

        protected override void OnDestroy()
        {
            if (m_CurrentTween != null)
            {
                m_CurrentTween.Kill();
                m_CurrentTween = null;
            }

            base.OnDestroy();
        }

        /// <summary>
        /// 评估事件数据并转换到适当的状态。
        /// </summary>
        public override void OnPointerUp(PointerEventData eventData)
        {
            base.OnPointerUp(eventData);

            if (m_IsScaling)
            {
                m_IsScaling = false;
            }

            // 还原到原始比例
            CacheRectTransform.localScale = m_OriginalScale;
        }

        /// <summary>
        /// 评估当前状态并转换到按下状态。
        /// </summary>s
        public override void OnPointerDown(PointerEventData eventData)
        {
            base.OnPointerDown(eventData);

            if (!m_IsPress || !m_IsInside)
            {
                return;
            }

            if (m_IsScaling)
            {
                // 取消动画
                m_CurrentTween.Kill();
                m_CurrentTween = null;

                // 上次缩放未完成, 回到初始状态
                m_IsScaling = false;

                CacheRectTransform.localScale = m_OriginalScale;
            }
            else
            {
                m_OriginalScale = CacheRectTransform.localScale;
                m_FinalScale = m_OriginalScale * k_ScaleFactor;
            }

            DoScale();
        }

        private void DoScale()
        {
            m_IsScaling = true;

            m_CurrentTween = CacheRectTransform.DOScale(m_FinalScale, k_ScaleDuration)
                .SetEase(Ease.InOutCirc)
                .OnComplete(OnScaleComplete);
        }

        private void OnScaleComplete()
        {
            m_CurrentTween = null;

            m_IsScaling = false;
            CacheRectTransform.localScale = m_OriginalScale;
        }
    }
}